<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Shoplevel extends Admin
{
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('shop_level')->where($map)->order('id desc')->paginate();

        // 分页数据
        $page = $data_list->render();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('店铺等级')// 设置页面标题
            ->setTableName('shop_level')// 设置数据表名
            ->setSearch(['shopname' => '店铺等级'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['id', '等级编号'],
                ['shopname', '店铺等级'],
                ['status', '当前状态','switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete')// 批量添加顶部按钮
            -> addTopButton('enable',['title'=>'启用'])
            -> addTopButton('disable',['title'=>'禁用'])
            ->addRightButtons('edit')// 批量添加右侧按钮
            ->addRightButton('delete',['icon' => 'fa fa-times', 'title' => '删除', 'href' => url('delete_level', ['id' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }

    //新增店铺等级
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ShopLevel');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            $data['createtime']=time();
            $res=Db::name('shop_level')->insertGetId($data);
            if ($res) {
                $details = "所属模块('shop'),操作日志('新增店铺等级'),等级ID('$res')";
                action_log('shoplevel_add', 'shop', $res, UID, $details);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增店铺等级')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'shopname', '会员等级', '必填，可由英文字母、数字组成'],
                ['radio', 'status', '店铺状态', '', ['禁用', '启用'], 1]
            ])
            ->fetch();
    }
    //编辑店铺等级
    public function edit($id = null){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ShopLevel');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            $res=Db::name('shop_level')->update($data);
            if ($res) {
                $details = "所属模块('shop'),操作日志('编辑店铺等级'),等级ID('$id')";
                action_log('shoplevel_edit', 'shop', $id, UID, $details);
                return $this->success('编辑成功', cookie('__forward__'));
            } else {
                return $this->error('编辑失败');
            }
        }
        $info = Db::name('shop_level')->where('id',$id)->find();
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('店铺等级编辑')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden','id'],
                ['text', 'shopname', '店铺等级', '必填'],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
    //单个删除店铺等级
    public function delete_level(){
        $id = $this->request->param('id');
        $res=Db::name("shop_level")->where('id',$id)->delete();
        if ($res) {
            $details = "所属模块('shop'),操作日志('删除店铺等级'),等级ID('$id')";
            action_log('shoplevel_delete', 'shop', $id, UID, $details);
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //批量删除店铺等级
    public function delete($record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $id=implode(',',$ids);
        foreach ($ids as $values){
            $res=Db::name("shop_level")->where('id',$values)->delete();
        }
        if ($res) {
            $details = "所属模块('shop'),操作日志('批量删除店铺等级'),等级ID('$id')";
            action_log('shoplevel_deletes', 'shop', $id, UID, $details);
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //批量审核
    public function enable($record = [])
    {
        $data=array('status'=>1);
        $this->auditing($data);

    }
    //批量审核禁用
    public function disable($record = [])
    {
        $data['status']=0;
        $this->auditing($data);

    }
    public function auditing($data){
        $ids= $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("shop_level")->where('id',$values)->update($data);
        }
        if ($res) {
            return $this->success('操作成功',cookie('__forward__'));
        } else {
            return $this->error('操作失败');
        }
    }
}